﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace FineSoft.ExtBuilder
{
	/// <summary>
	/// MainWindow.xaml 的交互逻辑
	/// </summary>
	public partial class Main : Window, System.ComponentModel.INotifyPropertyChanged, System.ComponentModel.INotifyPropertyChanging
	{
		public Main()
		{
			InitializeComponent();
		}

		private System.Windows.Controls.Grid _CurrentPageTab;
		protected System.Windows.Controls.Grid CurrentPageTab
		{
			get
			{
				if (this._CurrentPageTab == null)
					this._CurrentPageTab = (System.Windows.Controls.Grid)this.Page.Parent;
				return this._CurrentPageTab;
			}
		}

		private void TestMenu_Click(object sender, RoutedEventArgs e)
		{
			//this.WebBrowser.GetPositionOfComponent("ViewPort");
			//object result = this.WebBrowser.InvokeScript("eval", "(function(){return {a:1,b:2}})()");
			//int length = (int)result.GetType().InvokeMember("length", System.Reflection.BindingFlags.GetProperty, null, result, null);
		}

		//protected FineSoft.ViewModels.Page CurrentPageViewModel
		//{
		//    get
		//    {
		//        return (FineSoft.ViewModels.Page)((System.Windows.Controls.DockPanel)((System.Windows.Controls.Grid)((System.Windows.Controls.TabItem)this.PageTabContainer.Items[0]).Content).Children[0]).DataContext;
		//    }
		//}

		protected void Window_Loaded(object sender, RoutedEventArgs e)
		{
			//FineSoft.ViewModels.Page page = new ViewModels.Page(@"G:\Temp\Test.htm");
			//this.Page.Document = page.Document;
			//page.Document.OpenForEditor();
			//((System.Windows.Controls.DockPanel)((System.Windows.Controls.Grid)((System.Windows.Controls.TabItem)this.PageTabContainer.Items[0]).Content).Children[0]).DataContext = page;
			//this.WebBrowser.Source = new System.Uri(page.Document.EditorFilePath, UriKind.Absolute);

			//GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<System.Windows.Point>(this, this.ComponentGragOver);
			//GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<FineSoft.ViewModels.DropComponentInfo>(this, this.AddComponent);
			//GalaSoft.MvvmLight.Messaging.Messenger.Default.Register<FineSoft.Models.ComponentClass>(this, this.AddComponent);
		}

		//protected void AddComponent(FineSoft.ViewModels.DropComponentInfo dropComponentInfo)
		//{
		//    FineSoft.Models.ComponentObject component = this.CurrentPageViewModel.Document.AddComponent(this.SelectedComponent.NearestContainer, dropComponentInfo.ComponentClass);

		//    //dropComponentInfo.
		//    //FineSoft.Models.ComponentObject component = new Models.ComponentObject();
		//    //component.Type = dropComponentInfo.ComponentClass;
		//    //component.Id = this.CurrentPageViewModel.Document.ComponentAutoId;
		//    //component.ParentComponent = this.SelectedComponent;
		//    //((System.Collections.Generic.KeyValuePair<FineSoft.Models.ContainerConfig, System.Collections.Generic.List<FineSoft.Models.ComponentObject>>)this.ContainerList.SelectedItem).Value.Add(component);
		//    this.WebBrowser.RefreshComponentInItems(component.RefreshContainer.ParentComponent == null ? null : component.RefreshContainer.ParentComponent.Id, component.RefreshContainer.Id, component.RefreshContainer.ConfigsString);
		//    this.SelectComponent(component);
		//}

		//protected void AddComponent(FineSoft.Models.ComponentClass componentType)
		//{
		//    if (this.SelectedComponent == null)
		//        this._SelectedComponent = this.CurrentPageViewModel.Document.ViewPortObject;

		//    FineSoft.Models.ComponentObject component = this.CurrentPageViewModel.Document.AddComponent(this.SelectedComponent.NearestContainer, componentType);
		//    this.WebBrowser.RefreshComponentInItems(component.RefreshContainer.ParentComponent == null ? null : component.RefreshContainer.ParentComponent.Id, component.RefreshContainer.Id, component.RefreshContainer.ConfigsString);
		//    this.SelectComponent(component);
		//}

		//private FineSoft.Models.ComponentObject _SelectedComponent;
		//public FineSoft.Models.ComponentObject SelectedComponent
		//{
		//    get
		//    {
		//        return this._SelectedComponent;
		//    }
		//    set
		//    {
		//        this.RaisePropertyChanging("SelectedComponent");
		//        this._SelectedComponent = value;
		//        this.RaisePropertyChanged("SelectedComponent");
		//    }
		//}

		//protected void ComponentGragOver(System.Windows.Point position)
		//{
		//    string[] componentIds = this.WebBrowser.GetComponentIdsAtPosition(this.CurrentPageViewModel.Document.AllComponentIds, position);
		//    this.SelectComponent(this.CurrentPageViewModel.Document.AllComponents.First(c => c.Id == componentIds[componentIds.Length - 1]).NearestContainer);
		//}

		//private void Cover_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		//{
		//    System.Windows.Point position = e.GetPosition(this.Cover);
		//    position.Y -= 40;
		//    this.SelectComponent(position);
		//}

		//private void Cover_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
		//{
		//    this.Cover_MouseLeftButtonDown(sender, e);
		//}

		//protected void SelectComponent(System.Windows.Point position)
		//{
		//    string[] componentIds = this.WebBrowser.GetComponentIdsAtPosition(this.CurrentPageViewModel.Document.AllComponentIds, position);
		//    this.SelectComponent(this.CurrentPageViewModel.Document.AllComponents.First(c => c.Id == componentIds[componentIds.Length - 1]));
		//}

		//protected void SelectComponent(FineSoft.Models.ComponentObject component)
		//{
		//    if (this.SelectedComponent == component)
		//        return;
		//    this.SelectedComponent = component;
		//    this.Cover.ShowMask(this.WebBrowser.GetRectangleOfComponent(this.SelectedComponent.Id));
		//}

		//private void Window_LocationChanged(object sender, EventArgs e)
		//{
		//    //if (this.WindowState != WindowState.Normal)
		//    //    return;

		//    this.Popup.HorizontalOffset = 1;
		//    this.Popup.HorizontalOffset = 0;
		//    this.Popup.VerticalOffset = 1;
		//    this.Popup.VerticalOffset = 0;
		//}

		#region INotifyPropertyChanging 成员

		protected void RaisePropertyChanging(string name)
		{
			if (this.PropertyChanging != null)
				this.PropertyChanging(this, new System.ComponentModel.PropertyChangingEventArgs(name));
		}

		public event System.ComponentModel.PropertyChangingEventHandler PropertyChanging;

		#endregion

		#region INotifyPropertyChanged 成员

		protected void RaisePropertyChanged(string name)
		{
			if (this.PropertyChanged != null)
				this.PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(name));
		}

		public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

		#endregion

		private void OpenMenu_Click(object sender, RoutedEventArgs e)
		{

		}
	}
}